package com.vecspace.data.model;

/**分页信息
 * @version 1.0
 * @author wanghua
 */
public class Pagination implements IPagination{
	
	public Pagination() {
		
	}

	public Pagination(int pageIndex, int pageSize) {
		setPageIndex(pageIndex);
		setPageSize(pageSize);
	}
	/**
	 * 
	 * @param pageIndex
	 * @param pageSize
	 * @param totalRows
	 */
	public Pagination(int pageIndex, int pageSize, int totalRows) {
		this(pageIndex, totalRows);		
		this.totalRows = totalRows;
	}
	
	/**计算总页数*/
	public int computePages() {
		int pages = 1;
		if(pageSize > 0 && paging) {
			pages = (int)Math.ceil(1.0 * totalRows / pageSize);
		}
		return pages;
	}

	private boolean paging;
	@Override
	public boolean isPaged() {
		return paging;
	}
	@Override
	public void setPaged(boolean paging) {
		this.paging = paging;
	}

	/**当前是第几页 base 1，从1开始*/
	private int pageIndex;
	
	/**每一页的记录数*/
	private int pageSize;
	
	@Override
	public int getFirstRow() {
        int pageStart = getPageIndex() - 1;
        if ( pageStart < 0 ) {
            pageStart = 0;
        }
        return pageStart * getPageSize();
    }
	
	@Override
	public int getStartRows()
    {
    	int startRows = getFirstRow();
    	if ( getTotalRows() == 0 ) {
            startRows = 0;	
        }
    	return startRows;
    }
	
	@Override
    public int getEndRows() {
        int startRows = getFirstRow() + 1;
        int lastRows = startRows + pageSize - 1;
        if ( lastRows > totalRows ) {
            lastRows = totalRows;    
        }
        return lastRows;
    }
	
	/**总共有几页*/
	//private int pages;
	
	/**一共有多少行*/
	private int totalRows;
	
	@Override
	public int getPageIndex() {
		return pageIndex;
	}

	@Override
	public void setPageIndex(int pageIndex) {
		if(pageIndex >= 1) {
			this.pageIndex = pageIndex;
		}else {
			this.pageIndex = 1;
		}
	}

	@Override
	public int getPageSize() {
		return pageSize;
	}
	
	@Override
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
		if(pageSize > 0) {
			this.paging = true;
		}else {
			this.paging = false;	
		}
	}

	@Override
	public int getPages() {
		return computePages();
	}

//	/**总共有几页*/
//	public void setPages(int pages) {
//		this.pages = pages;
//	}

	@Override
	public int getTotalRows() {
		return totalRows;
	}

	@Override
	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}
}
